package com.zx.viewlearn.views;

import android.app.ActionBar;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.zx.viewlearn.R;


/**
 * @作者 zhoux
 * @创建时间 2018/1/9
 * @描述 自定义topbar
 * @修改时间 2018/1/9
 * @修改描述
 * @修改者 zhoux
 **/
public class TopBar extends RelativeLayout {
    //控件
    Button leftButton;
    Button rightButton;
    MyView7 titleView;

    //自定义属性
    String leftString;//左边文字
    String rightString;//右边文字
    String titleString;//标题文字
    boolean leftShow;//左边是否显示
    boolean rightShow;//右边是否显示
    Drawable leftBg;//左边背景
    Drawable rightBg;//右边背景
    int titleSize;//标题文字大小

    //布局位置
    LayoutParams leftParams;//
    LayoutParams titleParams;
    LayoutParams rightParams;

    OnClick mOnClick;//点击事件

    public TopBar(Context context) {
        super(context);
        init(context, null, 0);
    }

    public TopBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs, 0);
    }

    public TopBar(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init(context, attrs, defStyle);
    }

    private void init(Context context, AttributeSet attrs, int defStyle) {
        // TODO: 2018/1/9 add views
        leftButton = new Button(context);

        titleView = new MyView7(context);
        titleView.setGravity(Gravity.CENTER);//设置文字位置居中
        rightButton = new Button(context);

        // Load attributes
        final TypedArray a = getContext().obtainStyledAttributes(
                attrs, R.styleable.TopBar, defStyle, 0);
        leftString = a.getString(R.styleable.TopBar_leftString);
        leftBg = a.getDrawable(R.styleable.TopBar_leftBg);
        rightString = a.getString(R.styleable.TopBar_rightString);
        titleString = a.getString(R.styleable.TopBar_titleString);
        titleSize = a.getDimensionPixelSize(R.styleable.TopBar_titleSize, 14);
        leftShow = a.getBoolean(R.styleable.TopBar_leftShow, true);
        rightShow = a.getBoolean(R.styleable.TopBar_rightShow, false);
        rightBg = a.getDrawable(R.styleable.TopBar_rightBg);

        leftButton.setText(leftString);
        leftButton.setVisibility(leftShow ? VISIBLE : GONE);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
            leftButton.setBackground(leftBg);
        } else {
            leftButton.setBackgroundDrawable(leftBg);
        }
        titleView.setText(titleString);
        titleView.setTextSize(titleSize);
        rightButton.setText(rightString);
        rightButton.setVisibility(rightShow ? VISIBLE : GONE);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
            rightButton.setBackground(rightBg);
        } else {
            rightButton.setBackgroundDrawable(rightBg);
        }
        a.recycle();

        //
        addViews();

        //
        addListener();
    }

    public interface OnClick {
        void leftOnclick(View view);

        void rightOnclick(View view);

        void titleOnclick(View view);
    }


    public OnClick getmOnClick() {
        return mOnClick;
    }

    public void setmOnClick(OnClick mOnClick) {
        this.mOnClick = mOnClick;
    }

    private void addListener() {
        leftButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mOnClick != null) {
                    mOnClick.leftOnclick(v);
                }
            }
        });
        titleView.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mOnClick != null) {
                    mOnClick.titleOnclick(v);
                }
            }
        });
        rightButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mOnClick != null) {
                    mOnClick.rightOnclick(v);
                }
            }
        });
    }


    private void addViews() {
        leftParams = new LayoutParams(ActionBar.LayoutParams.WRAP_CONTENT, ActionBar.LayoutParams.MATCH_PARENT);
        leftParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT, TRUE);
        addView(leftButton, leftParams);

        titleParams = new LayoutParams(ActionBar.LayoutParams.WRAP_CONTENT, ActionBar.LayoutParams.MATCH_PARENT);
        titleParams.addRule(RelativeLayout.CENTER_IN_PARENT, TRUE);
        addView(titleView, titleParams);

        rightParams = new LayoutParams(ActionBar.LayoutParams.WRAP_CONTENT, ActionBar.LayoutParams.MATCH_PARENT);
        rightParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, TRUE);
        addView(rightButton, rightParams);
    }


}
